import os.path
import os
import time
import io
import json
import yaml
import requests
requests.packages.urllib3.disable_warnings()  # 如果不引入该方法，requests的verify=FLASE参数会报Warning
import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class TestMonitoringDashboard:
    @staticmethod
    def get_target_os_architecture(self):
        if datas.TARGET_OS_ARCHITECTURE:
            return datas.TARGET_OS_ARCHITECTURE
        else:
            os.popen('/usr/bin/yum install sshpass -y')
            time.sleep(1)
            cmd_get_os_architecture = \
                "sshpass -p '{ssh_password}' ssh -o StrictHostKeyChecking=no root@{target_ip} 'uname -m'"
            cmd_get_os_architecture = cmd_get_os_architecture.replace('{ssh_password}', datas.SSH_PASSWORD)
            cmd_get_os_architecture = cmd_get_os_architecture.replace('{target_ip}', datas.IP)
            os_architecture = os.popen(cmd_get_os_architecture).readlines()[-1]
            return os_architecture

    @staticmethod
    def get_alive_cookie(self):
        # 获取一个存活的cookie
        uri_get_alive_cookie = f'/clusters/{datas.test_openfuyao_ray["cluster_name"]}/rest/ray/rest/v1/overview/all'
        url_get_alive_cookie = datas.baseUrl + uri_get_alive_cookie
        r_get_alive_cookie = RequestUtil().send_request('GET', url_get_alive_cookie)
        request_head_get_alive_cookie = r_get_alive_cookie.request.headers
        alive_cookie = request_head_get_alive_cookie['Cookie']
        return alive_cookie

    @staticmethod
    def create_fluentbit_configmap(self):
        # 尝试获取名为"fluentbit-config"的ConfigMap，如果获取不到则创建
        uri_get_fluentbit_configmap = \
            f'/clusters/host/api/kubernetes/api/v1/namespaces/default/configmaps/fluentbit-config'
        url_get_fluentbit_configmap = datas.baseUrl + uri_get_fluentbit_configmap
        r_get_fluentbit_configmap = RequestUtil().send_request('GET', url_get_fluentbit_configmap)
        if r_get_fluentbit_configmap.status_code == 200:
            # fluentbit-config"的ConfigMap已存在，无需重复创建
            return None
        else:
            uri_create_fluentbit_configmap = f'/clusters/host/api/kubernetes/api/v1/namespaces/default/configmaps'
            url_create_fluentbit_configmap = datas.baseUrl + uri_create_fluentbit_configmap
            header = {
                "Content-Type": "application/json",
            }
            artifact_path = os.path.dirname(
                __file__) + '/../../../data/v25.03/openFuyao-Ray/Artifact_create_fluentbit-configmap.yaml'
            with open(artifact_path, 'r', encoding='utf-8') as file:
                yaml_content = yaml.safe_load(file)  # dict格式
            RequestUtil().send_request('POST', url_create_fluentbit_configmap, headers=header, json=yaml_content)
            return None

    # Overview概览
    @pytest.mark.parametrize('yaml_ray', 
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_overview_all.yaml'))
    def test_get_overview_all(self, yaml_ray):
        # 获取概览信息
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['code'] == 200  # 严格校验
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验

    # RayCluster
    @pytest.mark.parametrize('yaml_ray', 
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_create_raycluster.yaml'))
    def test_create_raycluster(self, yaml_ray):
        # 创建RayCluster资源
        # 由于该接口的body体要求以multipart/form-data方式发送参数，但又不能是文件形式。所以只能通过requests模块同时发送
        # data和files来实现（用files发送一个空文件来激活multipart/form-data，用data来发送真实参数）
        # 测试工程封装的RequestUtil().send_request(method, url, files=file, json=data)不能同时发送json和files。所以只能
        # 先拿到一个存货cookie，然后直接用requests模块实现
        # 先创建fluentbit的ConfigMap。这是25.06RayCluster资源激活“集群健康检测”功能的必要条件
        self.create_fluentbit_configmap(self)
        # 获取一个存活的cookie。用于发起创建RayCluster的POST请求
        alive_cookie = self.get_alive_cookie(self)
        # 创建RayCluster资源相关的url、请求头信息
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        # method = yaml_ray['request']['method']    # 在requests.post()中直接指定，不需要在这里指定
        # headers = yaml_ray['request']['headers']  # 同时发送data和files时，requests会自动生成Content-Type，所以不需要
        # 这里指定
        headers = {
            # 这里不能指定请求头,强行配置可能会产生错误。由于requests参数里面同时有data和files，requests会自动配置请求头
            # 'Content-Type': 'multipart/form-data; boundary=xxx'
            "Cookie": alive_cookie
        }
        # 获取目标OS架构
        os_architecture = self.get_target_os_architecture(self)  # os_architecture = datas.target_os_architecture
        # 不同架构对应不同的yaml（镜像不同）
        if 'x86_64' in os_architecture:
            artifact_path = os.path.dirname(
                __file__) + '/../../../data/v25.03/openFuyao-Ray/Artifact_create_raycluster_2506_x86.yaml'
        else:
            artifact_path = os.path.dirname(
                __file__) + '/../../../data/v25.03/openFuyao-Ray/Artifact_create_raycluster_2506_arm.yaml'
        with open(artifact_path, "r", encoding="utf-8") as file:
            yaml_content = file.read()
        data = {
            "config": yaml_content,  # YAML 内容作为字符串
            "isStart": "false",
            "createBy": "admin"
        }
        file = {  # 如果仅发送data参数而不发送files参数，则仅会有'multipart/form-data'，不会有'boundary=xxx'。所以这里
            # 搞一个空文件来激活'boundary=xxx'
            "dummy_file": (  # 表单字段名（可自定义）
                "dummy.txt",  # 文件名（服务器接收的文件名）
                io.BytesIO(b""),  # 空文件内容（二进制模式）
                "text/plain"  # MIME 类型（文本文件）
            )
        }
        req = requests.post(url, headers=headers, data=data, files=file, verify=False)
        # print(f'req.text: {req.text}')
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(1)

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_start_specified_raycluster.yaml'))
    def test_start_specified_raycluster(self, yaml_ray):
        # 根据命名空间和名称启动指定 RayCluster
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{raycluster_name}', datas.test_openfuyao_ray['raycluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(5)

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_stop_specified_raycluster.yaml'))
    def test_stop_specified_raycluster(self, yaml_ray):
        # 根据命名空间和名称终止指定 RayCluster
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{raycluster_name}', datas.test_openfuyao_ray['raycluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(2)

    @pytest.mark.parametrize('yaml_ray', YamlUtil().read_testcase_yaml(
        'v25.03/openFuyao-Ray/GET_get_raycluster_list_metadata.yaml'))
    def test_get_raycluster_list_metadata(self, yaml_ray):
        # 获取 RayCluster 列表 元信息
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_raycluster_list.yaml'))
    def test_get_raycluster_list(self, yaml_ray):
        # 查询全部 RayCluster 列表
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_specified_raycluster.yaml'))
    def test_get_specified_raycluster(self, yaml_ray):
        # 根据命名空间和名称获取指定 RayCluster
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{raycluster_name}', datas.test_openfuyao_ray['raycluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    #
    @pytest.mark.parametrize('yaml_ray', YamlUtil().read_testcase_yaml(
        'v25.03/openFuyao-Ray/DELETE_delete_specified_raycluster.yaml'))
    def test_delete_specified_raycluster(self, yaml_ray):
        # 根据命名空间和名称删除 RayCluster
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{raycluster_name}', datas.test_openfuyao_ray['raycluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    # RayJob
    @pytest.mark.parametrize('yaml_ray', YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_create_rayjob.yaml'))
    def test_create_rayjob(self, yaml_ray):
        # 根据配置文件创建 RayJob
        alive_cookie = self.get_alive_cookie(self)
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        headers = {"Cookie": alive_cookie}
        os_architecture = self.get_target_os_architecture(self)
        # 不同架构对应不同的yaml（镜像不同）
        if 'x86_64' in os_architecture:
            artifact_path = os.path.dirname(
                __file__) + '/../../../data/v25.03/openFuyao-Ray/Artifact_create_rayjob_2503_x86.yaml'
        else:
            artifact_path = os.path.dirname(
                __file__) + '/../../../data/v25.03/openFuyao-Ray/Artifact_create_rayjob_2503_arm.yaml'
        with open(artifact_path, "r", encoding="utf-8") as file:
            yaml_content = file.read()
        data = {
            "config": yaml_content,  # YAML 内容作为字符串
            "isStart": "false",
            "createBy": "admin"
        }
        file = {  # 如果仅发送data参数而不发送files参数，则仅会有'multipart/form-data'，不会有'boundary=xxx'。所以这里
            # 搞一个空文件来激活'boundary=xxx'
            "dummy_file": (  # 表单字段名（可自定义）
                "dummy.txt",  # 文件名（服务器接收的文件名）
                io.BytesIO(b""),  # 空文件内容（二进制模式）
                "text/plain"  # MIME 类型（文本文件）
            )
        }
        req = requests.post(url, headers=headers, data=data, files=file, verify=False)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(1)

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_start_specified_rayjob.yaml'))
    def test_start_specified_rayjob(self, yaml_ray):
        # 根据命名空间和名称启动指定 RayJob
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayjob_name}', datas.test_openfuyao_ray['rayjob_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(5)

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_stop_specified_rayjob.yaml'))
    def test_stop_specified_rayjob(self, yaml_ray):
        # 根据命名空间和名称终止指定 RayJob
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayjob_name}', datas.test_openfuyao_ray['rayjob_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(2)

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_rayjob_list_metadata.yaml'))
    def test_get_rayjob_list_metadata(self, yaml_ray):
        # 获取 RayJob 列表 元信息
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    #
    @pytest.mark.parametrize('yaml_ray', YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_rayjob_list.yaml'))
    def test_get_rayjob_list(self, yaml_ray):
        # 查询全部 RayJob 列表
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    #
    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_specified_rayjob.yaml'))
    def test_get_specified_rayjob(self, yaml_ray):
        # 根据命名空间和名称获取指定 RayJob
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayjob_name}', datas.test_openfuyao_ray['rayjob_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    #
    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/DELETE_delete_specified_rayjob.yaml'))
    def test_delete_specified_rayjob(self, yaml_ray):
        # 根据命名空间和名称删除 RayJob
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayjob_name}', datas.test_openfuyao_ray['rayjob_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    # RayService
    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_create_rayservice.yaml'))
    def test_create_rayservice(self, yaml_ray):
        # 根据配置文件创建 RayService
        alive_cookie = self.get_alive_cookie(self)
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        headers = {"Cookie": alive_cookie}
        os_architecture = self.get_target_os_architecture(self)
        # 不同架构对应不同的yaml（镜像不同）
        if 'x86_64' in os_architecture:
            artifact_path = os.path.dirname(
                __file__) + '/../../../data/v25.03/openFuyao-Ray/Artifact_create_rayservice_2503_x86.yaml'
        else:
            artifact_path = os.path.dirname(
                __file__) + '/../../../data/v25.03/openFuyao-Ray/Artifact_create_rayservice_2503_arm.yaml'
        with open(artifact_path, "r", encoding="utf-8") as file:
            yaml_content = file.read()
        data = {
            "config": yaml_content,  # YAML 内容作为字符串
            "isStart": "false",
            "createBy": "admin"
        }
        file = {  # 如果仅发送data参数而不发送files参数，则仅会有'multipart/form-data'，不会有'boundary=xxx'。所以这里
            # 搞一个空文件来激活'boundary=xxx'
            "dummy_file": (  # 表单字段名（可自定义）
                "dummy.txt",  # 文件名（服务器接收的文件名）
                io.BytesIO(b""),  # 空文件内容（二进制模式）
                "text/plain"  # MIME 类型（文本文件）
            )
        }
        req = requests.post(url, headers=headers, data=data, files=file, verify=False)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(1)

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_start_specified_rayservice.yaml'))
    def test_start_specified_rayservice(self, yaml_ray):
        # 根据命名空间和名称启动指定 RayService
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayservice_name}', datas.test_openfuyao_ray['rayservice_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(5)

    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/POST_stop_specified_rayservice.yaml'))
    def test_stop_specified_rayservice(self, yaml_ray):
        # 根据命名空间和名称终止指定 RayService
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayservice_name}', datas.test_openfuyao_ray['rayservice_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验
        time.sleep(2)

    @pytest.mark.parametrize('yaml_ray', YamlUtil().read_testcase_yaml(
        'v25.03/openFuyao-Ray/GET_get_rayservice_list_metadata.yaml'))
    def test_get_rayservice_list_metadata(self, yaml_ray):
        # 获取 RayService 列表 元信息
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    #
    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_rayservice_list.yaml'))
    def test_get_rayservice_list(self, yaml_ray):
        # 查询全部 RayService 列表
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    #
    @pytest.mark.parametrize('yaml_ray',
                             YamlUtil().read_testcase_yaml('v25.03/openFuyao-Ray/GET_get_specified_rayservice.yaml'))
    def test_get_specified_rayservice(self, yaml_ray):
        # 根据命名空间和名称获取指定 RayService
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayservice_name}', datas.test_openfuyao_ray['rayservice_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验

    #
    @pytest.mark.parametrize('yaml_ray', YamlUtil().read_testcase_yaml(
        'v25.03/openFuyao-Ray/DELETE_delete_specified_rayservice.yaml'))
    def test_delete_specified_rayservice(self, yaml_ray):
        # 根据命名空间和名称删除 RayService
        uri = yaml_ray['request']['url']
        uri = uri.replace('{cluster}', datas.test_openfuyao_ray['cluster_name'])
        uri = uri.replace('{namespace}', datas.test_openfuyao_ray['namespace'])
        uri = uri.replace('{rayservice_name}', datas.test_openfuyao_ray['rayservice_name'])
        base_url = datas.baseUrl
        url = base_url + uri
        method = yaml_ray['request']['method']
        headers = yaml_ray['request']['headers']
        data = yaml_ray['request']['data']
        req = RequestUtil().send_request(method, url, headers=headers, json=data)
        assert req.status_code == yaml_ray['validate'][0]['equals']['status_code']
        assert json.loads(req.text)['msg'] == 'success'  # 严格校验
        assert json.loads(req.text)['code'] == 200  # 严格校验